﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Security.Cryptography;

namespace Safety
{
    public class RSA
    {
        private static string localPublicKey = "<RSAKeyValue><Modulus>"
                + "wuqbVnf7C8xsKYWNQoOotmZM1aLHS7CNzG2ErAJky+jASnCtMVDgo38A6E3IyqeZotN2LKnChHaYRSBwX9JrptdHgGXFIeF6qLOG7r3ir"
                + "gSJIU3XAmohr2lEvNyfLmS6cJzyUuiODo+EsZ1ZwvpiOMHoc1hIUWM5a6rMpdrD/c8=</Modulus>"
                + "<Exponent>AQAB</Exponent></RSAKeyValue>";

        private static string localPrivateKey = "<RSAKeyValue><Modulus>wuqbVnf7C8xsKYWNQoOotmZM1aLHS7CNzG2ErAJky+jASnCtMVD"
            + "go38A6E3IyqeZotN2LKnChHaYRSBwX9JrptdHgGXFIeF6qLOG7r3irgSJIU3XAmohr2lEvNyfLmS6cJzyUuiODo+EsZ1Zwvpi"
            + "OMHoc1hIUWM5a6rMpdrD/c8=</Modulus><Exponent>AQAB</Exponent><P>+2J3L8+VOHNAGwyHUZG6dBJDXeLUhKiSclP"
            + "IK3yfMBg3MZdAcxgGaoxq3mwAX9+C92sQK0zhOQFGhwpDYUVWBw==</P><Q>xn68FOTNCrUXu0RiYDlZ+622ShXXITorNlZ6c"
            + "da4DwAEhCHHihuyBmlYL/ZftDJnKCftKTqes9+RE/PLn/Xn+Q==</Q><DP>OxApfLxDDR5XA4PwD0Q0EK1x0o9G61xH/ECfGU"
            + "iQhlZKMYaeB+yvJT2UpmmttNjpBoTLKcFIcYJRNc09g48s5Q==</DP><DQ>jbfYgzZkapunCO4nKrpn03hQKQ703OVSCuatX2"
            + "/FU/hYQRp5A9MIROmbrArEg7xkyFfRTNlsi9I4G725iUihyQ==</DQ><InverseQ>lErK597Cj8kp4UF2PYXncj9/RLq3ASJ9"
            + "miMIF7OliUIfjsaaHZnvT/36t3MBEqWDH9cs4JvvSkE9+CfezwNjNw==</InverseQ><D>TTVxYscUApBvJ+NnGTnd0qKjPNz"
            + "F5Q2Cnb+kFh68nqkLwnDvqeFHmnvseBiXuO66xOzOmtKD4sS3fGplGQ/Y5AOeCl9WqxCjurRleYpYrtVo+POTJvuFk22fw1zfR";


        /// <summary>
        /// Encrypt data of byte array type with RSA method
        /// </summary>
        /// <param name="publicKey">public key</param>
        /// <param name="encryptSource">waiting encrypt data</param>
        /// <returns></returns>
        public static byte[] RSAEncrypt(byte[] encryptSource)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(localPublicKey);
            encryptSource = rsa.Encrypt(encryptSource, false);
            return encryptSource;
        }

        /// <summary>
        /// Decrypt dataof byte array type with RSA method
        /// </summary>
        /// <param name="privateKey">private key</param>
        /// <param name="decryptSource">waiting decrypt data</param>
        /// <returns></returns>
        public static byte[] RSADecrypt(byte[] decryptSource)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(localPrivateKey);
            decryptSource = rsa.Decrypt(decryptSource, false);
            
            return decryptSource;
        }
    }
}
